package site.param.spring;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * Created by Administrator on 2017/1/4.
 */
public class ParameterInitBean implements BeanFactoryAware, BeanNameAware, InitializingBean, DisposableBean, ApplicationContextAware {

    private static Logger logger = LoggerFactory.getLogger(ParameterInitBean.class);
    private static ApplicationContext applicationContext;
    private String scanPackage;
    private String zookeepHost;
    private int loadType;
    private String httpUrl;
    private int retryCount;
    private int elapsedTimeMs;

    private BeanFactory beanFactory;
    private String beanName;

    public String getScanPackage() {
        return this.scanPackage;
    }

    public void setScanPackage(String scanPackage) {
        this.scanPackage = scanPackage;
    }

    public String getZookeepHost() {
        return this.zookeepHost;
    }

    public void setZookeepHost(String zookeepHost) {
        this.zookeepHost = zookeepHost;
    }

    public int getLoadType() {
        return this.loadType;
    }

    public void setLoadType(int loadType) {
        this.loadType = loadType;
    }

    public String getHttpUrl() {
        return this.httpUrl;
    }

    public void setHttpUrl(String httpUrl) {
        this.httpUrl = httpUrl;
    }

    public int getRetryCount() {
        return this.retryCount;
    }

    public void setRetryCount(int retryCount) {
        this.retryCount = retryCount;
    }

    public int getElapsedTimeMs() {
        return this.elapsedTimeMs;
    }

    public void setElapsedTimeMs(int elapsedTimeMs) {
        this.elapsedTimeMs = elapsedTimeMs;
    }

    public ParameterInitBean() {
        logger.info("initParameterBean  init ,load parameter !!!");
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("ParameterInitBean{");
        sb.append("scanPackage='").append(scanPackage).append('\'');
        sb.append(", zookeepHost='").append(zookeepHost).append('\'');
        sb.append(", loadType=").append(loadType);
        sb.append(", httpUrl='").append(httpUrl).append('\'');
        sb.append(", retryCount=").append(retryCount);
        sb.append(", elapsedTimeMs=").append(elapsedTimeMs);
        sb.append(", beanFactory=").append(beanFactory);
        sb.append(", beanName='").append(beanName).append('\'');
        sb.append('}');
        return sb.toString();
    }

    // 这是BeanFactoryAware接口方法
    @Override
    public void setBeanFactory(BeanFactory arg) throws BeansException {
        this.beanFactory = arg;
    }

    // 这是BeanNameAware接口方法
    @Override
    public void setBeanName(String arg) {
        this.beanName = arg;
    }

    // 这是InitializingBean接口方法
    @Override
    public void afterPropertiesSet() throws Exception {

    }

    // 这是DiposibleBean接口方法
    @Override
    public void destroy() throws Exception {

    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    /**
     * @return ApplicationContext
     */
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    /**
     * 获取对象
     *
     * @param name
     * @return Object
     * @throws BeansException
     */
    public static Object getBean(String name) throws BeansException {
        return applicationContext.getBean(name);
    }

    public static <T> T getBean(Class<T> c) throws BeansException {
        return (T) applicationContext.getBean(c);
    }
}
